package com.ibiaoke.marketing.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.StringUtils;
import com.ibiaoke.marketing.consts.CouponReceiveConsts;
import com.ibiaoke.marketing.entity.CouponReceive;
import com.ibiaoke.marketing.entity.CouponReceiveExample;
import com.ibiaoke.marketing.mapper.CouponReceiveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by huhaichao on 2017/6/14.
 */
@Repository
public class CouponReceiveDao extends BaseDao<CouponReceive, CouponReceiveExample, Integer, CouponReceiveMapper> {

    @Autowired
    private CouponReceiveMapper couponReceiveMapper;

    @Override
    public CouponReceiveMapper getMapper() {
        return couponReceiveMapper;
    }


    /**
     * @param customerId
     * @param fullPrice
     * @return
     */
    public List<CouponReceive> listCouponReceive(Integer customerId, BigDecimal fullPrice) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andUserIdEqualTo(customerId).andFullPriceLessThanOrEqualTo(fullPrice)
                .andUseStatusEqualTo((byte) CouponReceiveConsts.UNUSED)
                .andUseBeginTimeLessThanOrEqualTo(new Date()).andUseEndTimeGreaterThanOrEqualTo(new Date());
        example.setOrderByClause("receive_time desc");
        return selectByExample(example);
    }

    /**
     * 获取可使用的优惠券的列表
     *
     * @Param customerId
     * @Return List<CouponReceive>
     */
    public List<CouponReceive> listUnusedCouponReceive(Integer customerId) {

        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andUserIdEqualTo(customerId).andUseStatusEqualTo((byte) CouponReceiveConsts.UNUSED)
                .andUseBeginTimeLessThanOrEqualTo(new Date()).andUseEndTimeGreaterThanOrEqualTo(new Date());
        example.setOrderByClause("receive_time desc");
        return selectByExample(example);
    }

    /**
     * 获取已使用的优惠券的列表(未过期)
     *
     * @Param customerId
     * @Return
     */
    public List<CouponReceive> listUsedCouponReceive(Integer customerId) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andUserIdEqualTo(customerId).andUseStatusEqualTo((byte) CouponReceiveConsts.USED)
                .andUseBeginTimeLessThanOrEqualTo(new Date()).andUseEndTimeGreaterThanOrEqualTo(new Date());
        example.setOrderByClause("receive_time desc");
        return selectByExample(example);
    }


    /**
     * 获取已经过期的优惠券列表
     *
     * @Param customerId
     * @Return
     */
    public List<CouponReceive> listExpiredCouponReceive(Integer customerId) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andUserIdEqualTo(customerId).andUseEndTimeLessThan(new Date());
        example.setOrderByClause("receive_time desc");
        return selectByExample(example);
    }


    public List<CouponReceive> listCouponReceive(Integer couponId) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andCouponIdEqualTo(couponId);
        example.setOrderByClause("receive_time desc");
        return selectByExample(example);
    }

    public int useCouponReceive(Integer couponReceiveId) {
        CouponReceive couponReceive = new CouponReceive();
        couponReceive.setCouponReceiveId(couponReceiveId);
        couponReceive.setUseStatus((byte) CommonConsts.YES);
        return updateByPrimaryKeySelective(couponReceive);
    }

    public int releaseCoupon(Integer couponReceiveId) {
        CouponReceive couponReceive = new CouponReceive();
        couponReceive.setCouponReceiveId(couponReceiveId);
        couponReceive.setUseStatus((byte) CommonConsts.NO);
        return updateByPrimaryKeySelective(couponReceive);
    }

    /**
     * 查询领取的优惠券数量
     * @param userId
     * @param couponId
     * @param businessType
     * @param businessId
     * @return
     */
    public long countCoupon(Integer userId, Integer couponId, String businessType, Integer businessId) {
        CouponReceiveExample example = buildSearchExample(userId, couponId, businessType, businessId);
        return countByExample(example);
    }

    /**
     * 查询领取的优惠券列表
     * @param userId
     * @param couponId
     * @param businessType
     * @param businessId
     * @return
     */
    public List<CouponReceive> listCouponReceive(Integer userId, Integer couponId,String businessType, Integer businessId) {
        CouponReceiveExample example = buildSearchExample(userId, couponId, businessType, businessId);
        return selectByExample(example);
    }

    /**
     * 拼装查询条件
     * @param userId
     * @param couponId
     * @param businessType
     * @param businessId
     * @return
     */
    private CouponReceiveExample buildSearchExample(Integer userId, Integer couponId, String businessType, Integer businessId) {
        CouponReceiveExample example = new CouponReceiveExample();
        CouponReceiveExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (StringUtils.isNotEmpty(businessType)) {
            criteria.andBusinessTypeEqualTo(businessType);
        }
        if (businessType != null) {
            criteria.andBusinessIdEqualTo(businessId);
        }
        return example;
    }


    public int releaseCoupon(List<Integer> couponReceiveIds) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andCouponReceiveIdIn(couponReceiveIds);

        CouponReceive couponReceive = new CouponReceive();
        couponReceive.setUseStatus((byte) CommonConsts.NO);
        return updateByExampleSelective(couponReceive,example);
    }

    /**
     * 获取个人可用优惠券的数量
     * @param customerId
     * @return
     */
    public Long countUnusedCouponReceive(Integer customerId) {
        CouponReceiveExample example = new CouponReceiveExample();
        example.createCriteria().andUserIdEqualTo(customerId).andUseStatusEqualTo((byte) CouponReceiveConsts.UNUSED)
                .andUseBeginTimeLessThanOrEqualTo(new Date()).andUseEndTimeGreaterThanOrEqualTo(new Date());
        return countByExample(example);
    }
}
